package com.hpe.wx.performance.redis.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class PerformanceDaoRedisImpl implements PerformanceRedisDao {

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Override
	public String add(final String key, final String value) {
		int h = redisTemplate.hashCode();
		System.out.println("##############" + h);
		
		//http://blog.csdn.net/java2000_wl/article/details/8543203/
		
		redisTemplate.execute(new RedisCallback<Object>() {
			@Override
			public Object doInRedis(RedisConnection connection) throws DataAccessException {
				connection.set(redisTemplate.getStringSerializer().serialize("user.uid." + key),
						redisTemplate.getStringSerializer().serialize(value));
				//connection.setNX(key, value)
				return null;
			}
			
			
		});
		return null;
	}
	
	public void delete(String key) {  
        List<String> list = new ArrayList<String>();  
        list.add(key);  
        del(list);  
    } 
	public String del(List<String> key) {
		redisTemplate.delete(key);  
		return null;
	}

	@Override
	public String del(String key) {
		redisTemplate.delete(key);  
		return null;
	} 

	@Override
	public String update(String key, String value) {
//		 String key = user.getId();  
//	        if (get(key) == null) {  
//	            throw new NullPointerException("数据行不存在, key = " + key);  
//	        }  
//	        boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
//	            public Boolean doInRedis(RedisConnection connection)  
//	                    throws DataAccessException {  
//	                RedisSerializer<String> serializer = getRedisSerializer();  
//	                byte[] key  = serializer.serialize(user.getId());  
//	                byte[] name = serializer.serialize(user.getName());  
//	                connection.set(key, name);  
//	                return true;  
//	            }  
//	        });  
		return null;
	}

//	public User get(final String keyId) {  
//        User result = redisTemplate.execute(new RedisCallback<User>() {  
//            public User doInRedis(RedisConnection connection)  
//                    throws DataAccessException {  
//                RedisSerializer<String> serializer = getRedisSerializer();  
//                byte[] key = serializer.serialize(keyId);  
//                byte[] value = connection.get(key);  
//                if (value == null) {  
//                    return null;  
//                }  
//                String name = serializer.deserialize(value);  
//                return new User(keyId, name, null);  
//            }  
//        });  
//        return result;  
//    }  
	
	@Override
	public String query(String key) {
		// TODO Auto-generated method stub
		return null;
	}
}
